package com.cyq.day13.book;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class BookManagementSystem {
    Scanner scanner = new Scanner(System.in);
    Book[] book = new Book[3];

    public void init() {
        book[0] = new Book();
        book[0].num = 0;
        book[0].name = "星辰变";
        book[0].statu = false;
        book[0].borrowTime = 0;
        book[0].time = null;
        book[1] = new Book();
        book[1].num = 1;
        book[1].name = "斗破苍穹";
        book[1].statu = false;
        book[1].borrowTime = 0;
        book[1].time = null;
        book[2] = new Book();
        book[2].num = 2;
        book[2].name = "大主宰";
        book[2].statu = false;
        book[2].borrowTime = 0;
        book[2].time = null;
    }

    public void menu() {
        while (true) {
            System.out.println("欢迎使用图书管理系统");
            System.out.println("--------------------------");
            System.out.println("1.查看图书");
            System.out.println("2.新增图书");
            System.out.println("3.删除图书");
            System.out.println("4.借出图书");
            System.out.println("5.归还图书");
            System.out.println("6.图书借出排行榜");
            System.out.println("--------------------------");
            System.out.println("请选择(1-6):");
            int input = scanner.nextInt();
            switch (input) {
                case 0:
                    menu();
                    break;
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    borrow();
                    break;
                case 5:
                    giveback();
                    break;
                case 6:
                    borrowTimeBoard();
                    break;
                default:
                    System.out.println("输入有误请重新输入");
            }
        }
    }

    public void show() {
        System.out.println("----->查看图书");
        System.out.println("序号\t状态\t名称\t借出次数\t借出时间\t");
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null) {
                System.out.println((book[i].num) + "\t" + (book[i].statu ? "已借出" : "可借出") + "\t" + book[i].name + "\t" + book[i].borrowTime + "\t" +
                        book[i].time + "\t");
            }
        }

    }

    public void add() {
        System.out.println("----->新增图书");
        System.out.println("请输入图书的名字:");
        String input = scanner.next();
        Book[] newBook = new Book[book.length + 1];
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null) {
                newBook[i] = new Book();
                newBook[i].name = book[i].name;
                newBook[i].borrowTime = book[i].borrowTime;
                newBook[i].time = book[i].time;
                newBook[i].statu = book[i].statu;
            }
        }
        newBook[book.length] = new Book();
        newBook[book.length].name = input;
        newBook[book.length].statu = false;
        newBook[book.length].time = null;
        newBook[book.length].borrowTime = 0;
        book = newBook;
    }

    public void delete() {
        System.out.println("----->删除图书");
        System.out.println("请输入要删除图书的名字");
        String input = scanner.next();
        Book[] newBook = new Book[book.length - 1];
        int index = 0;
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null) {
                if (input.equals(book[i].name)) {
                    if (book[i].statu) {
                        System.out.println(book[i].name + "为借出状态，不能删除");
                        return;
                    }
                    index = i;
                }
            }
        }
        for (int j = 0; j < newBook.length; j++) {
            newBook[j] = new Book();
            if (book[j] != null) {
                if (j < index) {
                    newBook[j].name = book[j].name;
                    newBook[j].borrowTime = book[j].borrowTime;
                    newBook[j].time = book[j].time;
                    newBook[j].statu = book[j].statu;
                } else {
                    newBook[j].name = book[j + 1].name;
                    newBook[j].borrowTime = book[j + 1].borrowTime;
                    newBook[j].time = book[j + 1].time;
                    newBook[j].statu = book[j + 1].statu;
                }
            }
        }
        book = newBook;
    }

    public void borrow() {
        System.out.println("----->借出图书");
        System.out.println("请输入借出图书的名字");
        String input = scanner.next();
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null) {
                if (book[i].name.equals(input)) {
                    if (book[i].statu) {
                        System.out.println(book[i].name + "已经被借出了");
                    } else {
                        System.out.println("请输入借书日期（yyyy-MM-dd  年-月-日）:");
                        String inputTime = scanner.next();
                        System.out.println(book[i].name + "借出成功");
                        book[i].time = inputTime;
                        book[i].statu = true;
                        book[i].borrowTime++;
                    }
                    return;
                }
            }
        }
        System.out.println("没有找到匹配信息");

    }

    public void giveback() {
        System.out.println("----->归还图书");
        System.out.println("请输入要归还图书名字");
        String input = scanner.next();
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null) {
                if (input.equals(book[i].name)) {
                    if (!book[i].statu) {
                        System.out.println("该图书没有被借出，无法进行归还操作");
                        return;
                    } else {
                        System.out.println("请输入归还图书的日期(yyyy-MM-dd)");
                        String inputTime = scanner.next();
                        System.out.println("归还" + book[i].name + "成功");
                        System.out.println("借出时间是:" + book[i].time);
                        System.out.println("归还时间是:" + inputTime);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            Date d1 = sdf.parse(book[i].time);
                            Date d2 = sdf.parse(inputTime);

                            //d2.getTime() 得到str2的时间距离1970.1.1的毫秒数
                            //System.out.println(d2.getTime());

                            long diff = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
                            System.out.println("应付租金是:" + diff);
                            book[i].time = null;
                            book[i].statu = false;
                            return;
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        System.out.println("没有找到匹配信息");

    }

    public void borrowTimeBoard() {
        System.out.println("----->排行榜");
        System.out.println("名字" + "   " + "借出次数");
        for (int i = 0; i < book.length - 1; i++) {
            if (book[i] != null) {
                for (int j = 0; j < book.length - i - 1; j++) {
                    if (book[j + 1] != null) {
                        if (book[j].borrowTime < book[j + 1].borrowTime) {
                            int temp = book[j + 1].borrowTime;
                            book[j + 1].borrowTime = book[j].borrowTime;
                            book[j].borrowTime = temp;
                        }
                    }
                }
            }
        }
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null) {
                System.out.println(book[i].name + "  " + book[i].borrowTime);
            }
        }
    }
}
